package VWorldGameStudio;

import FCRendering.FCRBattleOrderConstant;
import FCRendering.FCRMaster;
import lombok.Getter;
import lombok.Setter;
import socket.FCRSocketClient;
import socket.FCRSocketServer;
import Function.FCMutilPlayCache;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Random;

import static FCRendering.FCRMaster.*;

/**
 * 主角，伙伴数据类
 */
public class FCPlayerInfo extends Sprite {
    public FCPlayerInfo() {
        aliasUID = FCRMaster.gameSave.HOST_UID;
    }

    @Setter
    @Getter
    private String aliasUID;

    @Setter
    @Getter
    /**
     * 默认不是npc，npc入队后，生成这个对象时，必须设置这个值
     */
    private boolean isANpcPlayer = false;

    // 选中角色
    private String selectPlayer = "player1";
    // 玩家名称
    @Getter
    @Setter
    private String name = "xixihehe";
    // 金额
    @Getter
    @Setter
    private long money = 50;
    // 经验
    @Getter
    @Setter
    private long experience = 0;
    // 等级
    @Getter
    @Setter
    private int level = 0;
    // 血量
    @Getter
    @Setter
    private int blood = 70;
    // 血量上限
    @Getter
    @Setter
    private int bloodMax = 70;
    // 暴击值-战斗值
    @Getter
    @Setter
    private int crit = 7;
    // 人物行走速度-速度最大值200
    @Getter
    @Setter
    private int speed1 = 40;
    // 坦克附加行走速度-驾驶 最大值应该是200,99级时，猎人每级升2点，其他都是1点
    @Getter
    @Setter
    private int speed2 = 10;
    // 修理成功数值-修理
    /**
     * //修理等级 猎人和女战士修理lv达到50以后可以使用修理破损装备，
     * 机械师天生可以修理破损装备，修理lv达到50以后可以修理大破装备。不过所有人都不能在战斗中修理。
     */
    @Getter
    @Setter
    private int repair = 10;

    // 抗毒、催眠等数值-强度
    @Getter
    @Setter
    private int strength = 10;

    //智力
    @Getter
    @Setter
    private int intelligence = 10;
    // 体力值-当前最大体力值
    @Getter
    @Setter
    private int physical = 10;
    // 剩余体力
    @Getter
    @Setter
    private int physicalnum = 10;


    /**
     * 人物背包，总容量10
     */
    @Getter
    @Setter
    private String[] knapsack = new String[10];
    // 装备武器
    @Getter
    @Setter
    private String arm;
    // 帽子
    @Getter
    @Setter
    private String hat;
    // 上衣
    @Getter
    @Setter
    private String jacket;
    // 防弹衣
    @Getter
    @Setter
    private String pants;

    // 手套
    @Getter
    @Setter
    private String glove;

    // 鞋子
    @Getter
    @Setter
    private String shoes;
    // 人物面朝方向 0向下 1向左  2向右， 3向上
    @Getter
    @Setter
    private volatile int ActionDir = 0;
    // 人物面朝方向所做动作编号
    @Getter
    @Setter
    private volatile int ActionNum = 0;
    //
    /**
     * 新增数据<br>
     * {暴击[0]最大88, 速度[1]200, 驾驶[2]150, 修理[3] 125, 强度[4]560, 智力[5], 体力上限[6], 血量上限[7]} 50=3000  90=5000
     */
    private int[] increase = new int[]{0, 0, 0, 0, 0, 0, 0, 0};

    /**
     * 体质<br>
     * {{角色1}, {角色2}, {角色3}} 猎人，修理，战士
     */
    private int[][] physique = new int[][]{{3, 2, 3, 1, 3, 1, 3, 45}, {1, 1, 2, 3, 5, 3, 3, 65},
            {2, 3, 1, 1, 8, 1, 3, 90}};
    @Getter
    @Setter
    private String occupation = "hunter";//职业

    @Getter
    @Setter
    private volatile String mapname = "home2";// 玩家1所在地图名

    @Setter
    @Getter
    private volatile int player1x = 9, player1y = 6;// 玩家1所在地图坐标

    @Setter
    @Getter
    private volatile int PianYiX = 0, PianYiY = 0;// 地图中心方块与画面中心的偏移量
    @Getter
    @Setter
    private String playertank1 = "null";// 玩家1当前驾驶的坦克ID

    @Getter
    @Setter
    private String playertankHostId = "null";// 玩家1当前驾驶的坦克HostId
    @Getter
    @Setter
    private boolean needCheckPerV = true;

    @Getter
    private LinkedList<String> fcActionQueue = new LinkedList<>();

    @Getter
    private HashMap<String, FCLongTermState> longTermMap = new HashMap<>();//战斗结束后遗留的状态放这里

    @Getter
    @Setter //记录最后一次出现在大地图的坐标XY
    private int lastInWorldMapX = -1;
    @Getter
    @Setter
    private int lastInWorldMapY = -1;


    @Getter
    @Setter
    private String talkingMsg;//正在的对话

    /**
     * 获取player1的所在coorx
     *
     * @return
     */
    public int getPlayer1CoorX() {
        return player1x;
    }

    /**
     * 获取player1的所在coory
     *
     * @return
     */
    public int getPlayer1CoorY() {
        return player1y;
    }

    private int getRand(Random random) {
        int r = random.nextInt(10001);
        return r == 0 ? 1 : r;
    }

    /**
     * 增加经验,反馈是否升级
     *
     * @param num
     * @return
     */
    public boolean addExperience(int num) {
        this.experience += num;
        int index = 0;
        if (selectPlayer.equals("player1")) {
            index = 0;
        } else if (selectPlayer.equals("player2")) {
            index = 1;
        } else if (selectPlayer.equals("player3")) {
            index = 2;
        }
        int newlevel = (int) Math.pow(experience / 10, 1 / 2.85);
        newlevel = level + 1;
        if (newlevel > level) {
            Random ran = new Random();
            increase[0] = (int) (50 / Math.sqrt(getRand(ran)) + ran.nextInt(physique[index][0]));
            increase[1] = (int) (30 / Math.sqrt(getRand(ran)) + ran.nextInt(physique[index][1]));
            increase[2] = (int) (70 / Math.sqrt(getRand(ran)) + ran.nextInt(physique[index][2]));
            increase[3] = (int) (40 / Math.sqrt(getRand(ran)) + ran.nextInt(physique[index][3]));
            increase[4] = (int) (100 / Math.sqrt(getRand(ran)) + ran.nextInt(physique[index][4]));
            increase[5] = (int) (40 / Math.sqrt(getRand(ran)) + ran.nextInt(physique[index][5]));
            increase[6] = (int) (20 / Math.sqrt(getRand(ran)) + ran.nextInt(physique[index][6]));
            increase[7] = (int) (300 / Math.sqrt(getRand(ran)) + ran.nextInt(physique[index][7]));
            for (int i = 0; i < 8; i++) {
                increase[i] = Math.max(0, increase[i]);
            }

            crit += increase[0] / 2;
            speed1 += increase[1];
            speed2 += increase[2];
            repair += increase[3];
            strength += increase[4];
            intelligence += increase[5];
            physical += increase[6];
            bloodMax += increase[7];
            level++;

            //System.out.println(getUpgradeData());
            String ti = "猎人";
            if (index == 1) {
                ti = "机械";
            } else if (index == 2) {
                ti = "士兵";
            }
            System.out.println(ti + "]：等级=" + level + " 战斗(暴击)值=" + crit + " 速度=" + speed1 + " 驾驶=" + speed2 + " 修理=" + repair
                    + " 强度=" + strength + " 智力=" + intelligence + " 体力=" + physical + " 最大血量=" + bloodMax);
            return true;
        }
        return false;
    }


    /**
     * 增加金钱
     *
     * @param money
     */
    public void addMoney(long money) {
        this.money += money;
    }

    /**
     * 获取升级参数
     *
     * @return
     */
    public String getUpgradeData2() {
        return "修理加" + increase[3] + ",强度加" + increase[4] + ",智力加" + increase[5] + ",体力加" + increase[6];
    }

    public String getUpgradeData1() {
        return "HP加" + increase[7] + ",战力加" + increase[0] + ",速度加" + increase[1] + ",驾驶加" + increase[2];
    }

    /**
     * 补充血量
     *
     * @param num
     */
    public void addBlood(int num) {
        blood += num;
        if (blood > bloodMax)
            blood = bloodMax;
    }

    /**
     * 给背包添加物品
     *
     * @param itemid
     * @return
     */
    public boolean addItemToKnapsack(String itemid) {
        for (int i = 0; i < knapsack.length; i++)
            if (knapsack[i] == null || knapsack[i].length() < 1 || knapsack[i].equals("null")) {
                knapsack[i] = itemid;
                return true;
            }
        return false;
    }

    public void subItemKnapsack(String id) {
        for (int i = 0; i < knapsack.length; i++)
            if (knapsack[i] != null && knapsack[i].equals(id)) {
                knapsack[i] = "null";
                return;
            }
    }

    /**
     * 根据背包位置丢弃物品
     *
     * @param num
     */
    public void subItemToKnapsackOfNum(int num) {
        if (num > -1 && num < 10)
            knapsack[num] = null;
    }

    /**
     * 根据num获取背包物品id
     *
     * @param num
     * @return
     */
    public String getKnapsackItemIDOfNum(int num) {
        return this.knapsack[num];
    }

    /**
     * @param
     * @return直接扣血
     */
    public void subBlood(int bd) {
        int newBlood = blood - bd;
        newBlood = Math.max(0, newBlood);
        blood = newBlood;
    }

    /**
     * 检测是否阵亡
     *
     * @return
     */
    public boolean isDeath() {
        return blood < 1;
    }

    /**
     * 恢复体力
     *
     * @param num
     */
    public void addPhysicalOfNum(int num) {
        this.physicalnum += num;
        if (physicalnum > physical)
            physicalnum = physical;
        if (physicalnum < 0)
            physicalnum = 0;
    }

    /**
     * 获取背包物品数量
     *
     * @return
     */
    public int getKnapsackItemNum() {
        int i = 0;
        for (String n : knapsack)
            if (n != null && n.indexOf("null") < 0)
                i++;
        return i;
    }


    private void inQueue(String actionInfo) {
        if (fcActionQueue.size() > 20) {
            fcActionQueue.pop();
        }
        fcActionQueue.addLast(actionInfo);
    }

    /**
     * 箱子拿物品去背包指定位置
     *
     * @param num
     * @param itemid
     * @return
     */
    public boolean itemMoveOfCase(int num, String itemid) {
        if (this.knapsack[num] == null || this.knapsack[num].indexOf("null") > -1) {
            this.knapsack[num] = itemid;
            return true;
        } else
            return false;
    }


    public void insertItemToKnapsack(int num, String itemid) {
        this.knapsack[num] = itemid;
    }


    public String getSelectPlayer() {
        //这里得整一个判断，如果是玩家人物就选以下三个，如果是npc人物就要显示npc的头像了
        if (isANpcPlayer) {
            return name;
        }
        return this.selectPlayer;
    }

    public void setSelectPlayer(int num) {
        switch (num) {
            case 1:
                this.selectPlayer = "player2";
                occupation = "machinist";
                break;
            case 2:
                this.selectPlayer = "player3";
                occupation = "warrior";
                break;
            default:
                this.selectPlayer = "player1";
                occupation = "hunter";
                break;
        }
    }


    public String getChName() {
        if (isANpcPlayer) {
            return langLib.getNameOfId(name);
        }
        return name;
    }

    //这个是别的玩家的数据控制
    public void setActionNumFromRemote(int num) {
        ActionNum = num;
    }

    //这个是别的玩家的数据控制
    public void setPianYiXFromRemote(int offset) {
        PianYiX = offset;
    }

    //这个是别的玩家的数据控制
    public void setPianYiYFromRemote(int offset) {
        PianYiY = offset;
    }


    public String getLevelForString() {
        return "LEVEL " + String.valueOf(level);
    }

    public String getMoneyForString() {
        return "MONEY " + String.valueOf(money);
    }

    public String getBloodForString() {
        return "HP " + String.valueOf(blood);
    }

    public double getBloodPRO() {
        return (double) blood / bloodMax;
    }

    public double getExperiencePRO() {
        long n = (level > 0) ? (long) Math.pow(level, 2.85) * 10 : 0;
        long n1 = (long) Math.pow(level + 1, 2.85) * 10;
        return (double) (experience - n) / (n1 - n);
    }

    public void setActionNum(int num) {
        //TODO 注意，如果是加入了别人的队伍，不能让设置这个值
        if (!fcv.isInTeamLink()) {
            ActionNum = (num > 3) ? 0 : num;
        }
    }

    public void disPatchActionNum(FCRPlayerLink link, int num) {
        //TODO 注意，如果是加入了别人的队伍，不能让调这个接口
        if (!fcv.isInTeamLink()) {
            if (link == null) {
                return;
            }
            FCPlayerInfo tail = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
            if (tail != null) {
                tail.setActionNum(num);
            }
            disPatchActionNum(link.nextPlayer, num);

            if (link.nextPlayer == null) {
                for (FCPropulsion propulsion : fcv.getPropulsions()) {
                    propulsion.setPactionNum(num);
                }
            }
        }
    }

    //加入队伍 用名称来防止冲突吧

    /**
     * 组队加入队伍后，也必须调用FCMutilPlayCache。joinGame加入到缓存去方便查调用
     *
     * @param playerInfo
     */
    public boolean joinInTeam(FCPlayerInfo playerInfo) {
        if (FCMutilPlayCache.getTeamMembers().size() == FCMutilPlayCache.MAX_TEAM_MEMBERS) {
            System.out.println("组队人数已经达到最大");
            return false;
        }

        playerInfo.setMapNameOfPlayer(mapname, player1x, player1y);
        playerInfo.setActionDir(ActionDir);
        playerInfo.setActionNum(ActionNum);
        if (FCMutilPlayCache.teamLink != null) {
            if (checkHasPlayer(FCMutilPlayCache.teamLink, playerInfo) != null) {
                //已经在队伍里了那就不用做啥了
            } else {
                FCRPlayerLink link = new FCRPlayerLink();
                link.curPlayerName = playerInfo.getName();
                link.curPlayerHostUID = playerInfo.getAliasUID();
                FCMutilPlayCache.teamLinkEnd.nextPlayer = link;
                link.forwardPlayer = FCMutilPlayCache.teamLinkEnd;
                FCMutilPlayCache.teamLinkEnd = link;
                FCMutilPlayCache.teamMembers.add(link);
            }
        } else {
            FCRPlayerLink link = new FCRPlayerLink();
            link.curPlayerName = playerInfo.getName();
            link.curPlayerHostUID = playerInfo.getAliasUID();
            FCMutilPlayCache.teamLink = link;
            FCMutilPlayCache.teamLinkEnd = link;
            FCMutilPlayCache.teamMembers.add(link);
        }

        //判断UID，是自己的npc就要发
        if (playerInfo.getAliasUID().equals(FCRMaster.gameSave.HOST_UID)) {
            if (FCRMaster.fcv.isSever()) {
                FCRSocketServer.getIn().reCreatePlayerAgian();
            } else if (FCRMaster.fcv.isClient()) {
                FCRSocketClient.getIn().reCreatePlayerAgian();
                //把自己的队伍人物同步给联机的人显示在屏幕上
            }
        }
        System.out.println("加入 组队人数=" + FCMutilPlayCache.getTeamMembers().size());
        return true;
    }

    //退出队伍
    public void quitTeam(FCPlayerInfo playerInfo) {
        FCRPlayerLink link = checkHasPlayer(FCMutilPlayCache.teamLink, playerInfo);
        if (link != null) {
            //那就去掉
            FCRPlayerLink forward = link.forwardPlayer;
            FCRPlayerLink next = link.nextPlayer;
            if (forward != null) {
                forward.nextPlayer = next;
                if (next != null) {
                    next.forwardPlayer = forward;
                }
                //刚好是队尾要去掉
                if (FCMutilPlayCache.teamLinkEnd == link) {
                    FCMutilPlayCache.teamLinkEnd = forward;
                }
                FCMutilPlayCache.teamMembers.remove(link);
            } else {
                //那就是队头了啊，没前面的
                FCMutilPlayCache.teamLink = next;
                FCMutilPlayCache.teamMembers.remove(link);
                if (FCMutilPlayCache.teamMembers.size() == 0) {
                    FCMutilPlayCache.teamLinkEnd = null;
                }
            }

            //TEST 打印一下队连
            printTeamLink(FCMutilPlayCache.teamLink);
        } else {
            System.out.println("quitTeam 没找到这个人" + playerInfo.getName() + " " + playerInfo.getAliasUID());
        }
        System.out.println("退出 组队人数=" + FCMutilPlayCache.getTeamMembers().size());
    }

    private void printTeamLink(FCRPlayerLink link) {
        if (link == null) {
            return;
        }
        System.out.println("====" + link.curPlayerHostUID + " name:" + link.curPlayerName);
        printTeamLink(link.nextPlayer);
    }

    //返回队伍中是否有此人
    private FCRPlayerLink checkHasPlayer(FCRPlayerLink link, FCPlayerInfo playerInfo) {
        if (link == null) {
            return null;
        }
        if (playerInfo.getName().equals(link.curPlayerName) && playerInfo.getAliasUID().equals(link.curPlayerHostUID)) {
            return link;
        }
        return checkHasPlayer(link.nextPlayer, playerInfo);
    }


    public void setMapNameOfPlayer(String mapname, int x, int y) {
        //TODO isInTeamLink true就在别人控制中，
        if (fcv.isInTeamLink()) {
            return;
        }
        if (this.mapname.equals("world")) {
            lastInWorldMapX = this.player1x;
            lastInWorldMapY = this.player1y;
        }

        this.player1x = x;
        this.player1y = y;
        this.mapname = mapname;
        getFcActionQueue().clear();
        String actionRecord = getPianYiX() + "," + getPianYiY() + "," + player1x + "," + player1y + "," + getActionDir();
        for(int i = 0; i < 20;i++) {
            inQueue(actionRecord);
        }
        for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
            FCPlayerInfo playerInfo = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
            if (playerInfo != null) {
                playerInfo.setMapname(mapname);
                playerInfo.player1x = x;
                playerInfo.player1y = y;
                playerInfo.setActionDir(ActionDir);
                playerInfo.setActionNum(ActionNum);
                playerInfo.getFcActionQueue().clear();
                for(int i = 0; i < 20;i++) {
                    playerInfo.inQueue(actionRecord);
                }
            }
        }

    }


    /**
     * bpbwan
     *
     * @param link
     */
    private void dispatchsetPianYi(FCPlayerInfo head, FCRPlayerLink link, int perV) {
        if (link == null) {
            //到尾巴了
            //牵引物
            LinkedList<String> actionQueue = head.getFcActionQueue();
            for (FCPropulsion propulsion : fcv.getPropulsions()) {
                if (actionQueue.size() > perV) {
                    String action = actionQueue.get(perV);//获取第perV个
                    propulsion.inQueueAction();
                    propulsion.setPactionNum(getActionNum());
                    propulsion.setPianYi(action, getMapname());
                    actionQueue = propulsion.getFcActionQueue();
                }
            }
            return;
        }
        FCPlayerInfo tail = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
        if (tail == null) {
            return;
        }
        if (head.getFcActionQueue().size() > perV) {
            String action = head.getFcActionQueue().get(perV);//获取第perV个
            String[] splits = action.split(",");
            tail.setPianYiX(Integer.parseInt(splits[0]));
            tail.setPianYiY(Integer.parseInt(splits[1]));
            tail.player1x = Integer.parseInt(splits[2]);
            tail.player1y = Integer.parseInt(splits[3]);
            tail.setActionDir(Integer.parseInt(splits[4]));
            tail.inQueue(action);
            //System.out.println("设置了 "+action);
            if (tail.getPianYiX() != getPianYiX() || tail.getPianYiY() != getPianYiY()) {
                setNeedCheckPerV(true);
            }
        }
        dispatchsetPianYi(tail, link.nextPlayer, perV);
    }

    public void addPropulsion(FCPropulsion fc) {
        if (!fcv.isInTeamLink()) {
            //检查队伍有没坦克车
            boolean canadd = false;
            if (getPlayertank1() != null && !getPlayertank1().equals("null")) {
                canadd = true;
            }
            if (!canadd) {
                for (FCRPlayerLink link : FCMutilPlayCache.getTeamMembers()) {
                    FCPlayerInfo p = FCMutilPlayCache.findPlayerByName(link.curPlayerHostUID, link.curPlayerName);
                    if (p != null && p.getPlayertank1() != null && !p.getPlayertank1().equals("null")) {
                        canadd = true;
                        break;
                    }
                }
            }
            if (!canadd) {
                fcv.getMessage().add("很抱歉，你没有战车可以牵引它!");
                fcv.showMessage();
                return;
            }
            if (fc.getVisable() == 0) {//正在使用中喔
                fcv.getMessage().add("很抱歉，此战车正在驾驶中!");
                fcv.showMessage();
                return;
            }
            FCPropulsion find = fcv.findPropulsionByHostId(fc.getPropulsionHostId());

            if (find == null) {
                fcv.getPropulsions().add(fc);
                fc.setIsBeingTow(true);
                dispatchsetPianYi(this, FCMutilPlayCache.teamLink, FCRMaster.gameSave.perV);
            }
        }
    }

    //加入队伍加入牵引物
    public void addRemotePropulsion(FCPropulsion fc) {
        if (!fcv.isInTeamLink()) {
            FCPropulsion find = fcv.findPropulsionByHostId(fc.getPropulsionHostId());
            if (find == null) {
                fcv.getPropulsions().add(fc);
                fc.setIsBeingTow(true);
                dispatchsetPianYi(this, FCMutilPlayCache.teamLink, FCRMaster.gameSave.perV);
            }
        }
    }

    public void removePropulsion() {
        if (!fcv.isInTeamLink()) {
            FCPropulsion find = fcv.findPropulsionByHostId(gameSave.HOST_UID);
            if (find != null) {
                fcv.getPropulsions().remove(find);
                find.setIsBeingTow(false);
            }
            System.out.println("removePropulsion " + find + "  SIZE:" + fcv.getPropulsions().size());
        }
    }

    //退出队伍移除牵引物
    public void removeRemotePropulsion(String hostId) {
        if (!fcv.isInTeamLink()) {
            FCPropulsion find = fcv.findPropulsionByHostId(hostId);
            if (find != null) {
                find.setIsBeingTow(false);
                fcv.getPropulsions().remove(find);
                dispatchsetPianYi(this, FCMutilPlayCache.teamLink, FCRMaster.gameSave.perV);
            }
        }
    }

    /**
     * TODO 要求在离开了别人的队伍后，要调用一次重整牵引物坐标
     */
    public void resetPropulsionIfNeed() {
        if (!fcv.isInTeamLink()) {
            FCPropulsion find = fcv.findPropulsionByHostId(getAliasUID());
            if (find != null) {
                dispatchsetPianYi(this, FCMutilPlayCache.teamLink, FCRMaster.gameSave.perV);
            }
        }
    }

    public void recordAndSetUpPianyi() {
        //记录
        String actionRecord = getPianYiX() + "," + getPianYiY() + "," + player1x + "," + player1y + "," + getActionDir();
        inQueue(actionRecord);
        if (!fcv.isInTeamLink()) {
            System.out.println("FCRMaster.gameSave.perV " + FCRMaster.gameSave.perV);
//            if (needCheckPerV) {
//                for (int i = 0; i < getFcActionQueue().size(); i++) {
//                    String action = getFcActionQueue().get(i);
//                    if (action != null) {
//                        String[] splits = action.split(",");
//                        if (player1x != Integer.parseInt(splits[2]) || player1y != Integer.parseInt(splits[3])) {
//                            if (getPianYiY() == Integer.parseInt(splits[1]) && getPianYiX() == Integer.parseInt(splits[0])) {
//                       //         FCRMaster.gameSave.perV = i;
//                                System.out.println("FCRMaster.gameSave.perV " + FCRMaster.gameSave.perV);
//                                needCheckPerV = false;
//                                break;
//                            }
//                        }
//                    }
//                }
//            }
            //     System.out.println("leader "+actionRecord+" perV "+FCRMaster.gameSave.perV+" needCheckPerV:"+needCheckPerV);
            dispatchsetPianYi(this, FCMutilPlayCache.teamLink, FCRMaster.gameSave.perV);
        }
    }


    public void setPianYiXAndDispatchPianYI(int ox) {
        setPianYiX(ox);
        recordAndSetUpPianyi();
    }

    public void setPianYiYAndDispatchPianYI(int oy) {
        setPianYiY(oy);
        recordAndSetUpPianyi();
    }


    /**
     * 移动player1的coor
     *
     * @param x
     * @param y
     */
    public void movePlayer1Coor(int x, int y) {
        disPatchActionNum(FCMutilPlayCache.teamLink, getActionNum());
        player1x += x;
        player1y += y;
    }

    //获取人物的物理攻击
    public int getPlayerAttackHurt() {
        FCItem item = itemLib.getItemOfID(arm);
        if (item != null) {
            return (int) (item.getAttackHurt() + getStrength() * 0.25);
        } else {
            return (int) (getStrength() * 1.25);
        }
    }

    //TODO 获取暴击率
    public int getCritRate(FCPlayerBattleWrap wrap) {
        int cirtRate = wrap.getLongTermVal(FCRBattleOrderConstant.S_AddCrit);
        cirtRate = cirtRate == -1 ? 0 : cirtRate;
        cirtRate += getCrit();//暴击值（最大100）
        String[] fangJu = new String[]{hat, jacket, shoes, glove};
        for (int i = 0; i < fangJu.length; i++) {
            FCItem item = itemLib.getItemOfID(fangJu[i]);
            if (item != null) {
                cirtRate += (int) (100 * item.getCrit());
            }
        }
        cirtRate = Math.max(0, cirtRate);
        cirtRate = Math.min(100, cirtRate);
        return cirtRate;
    }


    //所有人物战斗开始命中
    //TODO 获取命中率
    public int getHitRate(FCPlayerBattleWrap wrap) {
        double hitRate = 0;
        String[] fangJu = new String[]{hat, jacket, shoes, glove};
        for (int i = 0; i < fangJu.length; i++) {
            FCItem item = itemLib.getItemOfID(fangJu[i]);
            if (item != null) {
                hitRate += item.getHitRate();
            }
        }
        hitRate = Math.min(100, hitRate);

        hitRate = hitRate / 100 * 0.2;
        int buffHitRate = wrap.getLongTermVal(FCRBattleOrderConstant.S_AddHitRate);
        if (buffHitRate != -1) {
            buffHitRate = Math.min(100, buffHitRate);
            hitRate += (double) buffHitRate / 100 * 0.1;
        }
        FCItem armItem = itemLib.getItemOfID(arm);
        if (armItem != null) {
            int armHitrate = Math.min(100, armItem.getHitRate());
            hitRate += (double) armHitrate / 100 * 0.2;
        }
        hitRate = hitRate + 0.9 + level * 0.001;
        int isGetFog = wrap.getLongTermVal(FCRBattleOrderConstant.S_Fog);
        if (isGetFog != -1) {
            hitRate /= 2;
        }
        return (int) (hitRate * 100);
    }

    //获取闪避率
    public int getDogeRate() {
        double doges = 0;
        String[] fangJu = new String[]{hat, jacket, shoes, glove};
        for (int i = 0; i < fangJu.length; i++) {
            FCItem item = itemLib.getItemOfID(fangJu[i]);
            if (item != null) {
                doges += Math.min(50, item.getDodgeRateAdd());
            }
        }
        doges = doges / 100 * 0.3;
        doges += speed1 * 0.0005;
        return (int) (doges * 100);
    }

    //TODO 获取战斗速度
    public int getBattleSpeed(FCPlayerBattleWrap wrap) {
        int speed = wrap.getLongTermVal(FCRBattleOrderConstant.S_AddSpeed);
        speed = speed == -1 ? 0 : speed;
        speed += getSpeed1();
        String[] fangJu = new String[]{hat, jacket, shoes, glove};
        for (int i = 0; i < fangJu.length; i++) {
            FCItem item = itemLib.getItemOfID(fangJu[i]);
            if (item != null) {
                speed += item.getSpeedAdd();
            }
        }
        return speed;
    }

    //TODO 获取人物包括防甲的物理防御
    public int getPlayerWuliDefend() {
        int defend = 0;
        String[] fangJu = new String[]{hat, jacket, pants, shoes, glove};
        for (int i = 0; i < fangJu.length; i++) {
            FCItem item = itemLib.getItemOfID(fangJu[i]);
            if (item != null) {
                defend += item.getCurrentDefense();
            }
        }
        return defend;
    }


    //TODO 获取酸抗性
    public int getPlayerAcidDefend() {
        int defend = 0;
        String[] fangJu = new String[]{hat, jacket, shoes, glove};
        for (int i = 0; i < fangJu.length; i++) {
            FCItem item = itemLib.getItemOfID(fangJu[i]);
            if (item != null && FCRBattleOrderConstant.S_Acid.equals(item.getElements())) {
                defend += item.getElementsValue();
            }
        }
        //不能超过100抗性
        defend = Math.min(100, defend);
        defend = Math.max(20, defend);
        return defend;
    }

    //TODO 获取火抗性
    public int getPlayerFireDefend() {
        int defend = 0;
        String[] fangJu = new String[]{hat, jacket, shoes, glove};
        for (int i = 0; i < fangJu.length; i++) {
            FCItem item = itemLib.getItemOfID(fangJu[i]);
            if (item != null && FCRBattleOrderConstant.S_Fire.equals(item.getElements())) {
                defend += item.getElementsValue();
            }
        }
        defend = Math.min(100, defend);
        defend = Math.max(20, defend);
        return defend;
    }

    //TODO 获取毒抗性
    public int getPlayerGasDefend() {
        int defend = 0;
        String[] fangJu = new String[]{hat, jacket, shoes, glove};
        for (int i = 0; i < fangJu.length; i++) {
            FCItem item = itemLib.getItemOfID(fangJu[i]);
            if (item != null && FCRBattleOrderConstant.S_Gas.equals(item.getElements())) {
                defend += item.getElementsValue();
            }
        }
        defend = Math.min(100, defend);
        defend = Math.max(20, defend);
        return defend;
    }

    //TODO 获取睡眠抗性
    public int getPlayerSleepDefend() {
        int defend = 0;
        String[] fangJu = new String[]{hat, jacket, shoes, glove};
        for (int i = 0; i < fangJu.length; i++) {
            FCItem item = itemLib.getItemOfID(fangJu[i]);
            if (item != null && FCRBattleOrderConstant.S_Sleep.equals(item.getElements())) {
                defend += item.getElementsValue();
            }
        }
        defend = Math.min(100, defend);
        defend = Math.max(20, defend);
        return defend;
    }

    //TODO 获取麻痹抗性
    public int getPlayerParalysisDefend() {
        int defend = 0;
        String[] fangJu = new String[]{hat, jacket, shoes, glove};
        for (int i = 0; i < fangJu.length; i++) {
            FCItem item = itemLib.getItemOfID(fangJu[i]);
            if (item != null && FCRBattleOrderConstant.S_Paralysis.equals(item.getElements())) {
                defend += item.getElementsValue();
            }
        }
        defend = Math.min(100, defend);
        defend = Math.max(20, defend);
        return defend;
    }

    //TODO 获取冰冻抗性
    public int getPlayerFrozenDefend() {
        int defend = 0;
        String[] fangJu = new String[]{hat, jacket, shoes, glove};
        for (int i = 0; i < fangJu.length; i++) {
            FCItem item = itemLib.getItemOfID(fangJu[i]);
            if (item != null && FCRBattleOrderConstant.S_Frozen.equals(item.getElements())) {
                defend += item.getElementsValue();
            }
        }
        defend = Math.min(100, defend);
        defend = Math.max(20, defend);
        return defend;
    }

}
